package cn.patient.pp.module.patients.dal.mysql.patients;

import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.patient.pp.framework.mybatis.core.mapper.BaseMapperX;
import cn.patient.pp.module.patients.controller.admin.vo.*;
import cn.patient.pp.module.patients.controller.admin.vo.importExecl.PatientsDOExeclVO;
import cn.patient.pp.module.patients.dal.dataobject.patients.PatientsDODO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.*;


/**
 * 患者总 Mapper
 *
 * @author sailv
 */
@Mapper
public interface PatientsDOMapper extends BaseMapperX<PatientsDODO> {

    default PageResult<PatientsDODO> selectPage(PatientsDOPageReqVO reqVO,
                                                List<Integer> ids) {
        // 构造公共查询条件
        LambdaQueryWrapperX<PatientsDODO> queryWrapperX = new LambdaQueryWrapperX<>();
        queryWrapperX.inIfPresent(PatientsDODO::getDeptId, reqVO.getDeptIds())
                .likeIfPresent(PatientsDODO::getPatientName, reqVO.getPatientName())
                .eqIfPresent(PatientsDODO::getPatientCode, reqVO.getPatientCode())
                .eqIfPresent(PatientsDODO::getIdNumber, reqVO.getIdNumber())
                .betweenIfPresent(PatientsDODO::getNextFollowupDate, reqVO.getNextFollowupDate())
                .eqIfPresent(PatientsDODO::getGender, reqVO.getGender())
                .likeIfPresent(PatientsDODO::getCurrentAddress, reqVO.getCurrentAddress())
                .betweenIfPresent(PatientsDODO::getLastFollowupDate, reqVO.getLastFollowupDate())
                .eqIfPresent(PatientsDODO::getHospitalizationStatus, reqVO.getHospitalizationStatus())
                .eqIfPresent(PatientsDODO::getDangerousBehavior, reqVO.getDangerousBehavior())
                .eqIfPresent(PatientsDODO::getRiskAssessment, reqVO.getRiskAssessment())
                .inIfPresent(PatientsDODO::getId, ids);

        // 如果是警务角色，再加风险级别过滤
//        if (hasPoliceRole) {
//            queryWrapperX.in(PatientsDODO::getRiskAssessment, "3", "4", "5");
//        }

        // 排序、分页
        queryWrapperX.orderByDesc(PatientsDODO::getCreateTime);
        return selectPage(reqVO, queryWrapperX);
    }

    List<Integer> selectLatestIds(@Param("pageReqVO") PatientsDOPageReqVO pageReqVO);

    List<Integer> selectLatestIdsNew(@Param("pageReqVO") PatientsDOPageReqVO pageReqVO,@Param("deptIds") List<Long> deptIds);

    void insertIgnoreBatch(@Param("list") List<PatientsDOExeclVO> list);

    /** 患者总数 */
    Long selectTotalPatientsByType(Map<String,Object> params);

    /** 各类型患者数 */
    List<PatientTypeCountVO> selectPatientTypeCounts(Map<String,Object> params);

    List<AgeCountVO> selectPatientAgeCounts(Map<String, Object> params);

    /** 删除基本信息表数据，patientCode为18位0的 */
    Integer deleteByIdsAndPatientCode(@Param("idNumbers") Set<String> idNumbers,
                              @Param("patientCode") String patientCode);

    List<Map<String, Object>> selectLatestRiskCounts(@Param("deptIds") List<Long> deptIds);

    List<PatientsDODO> selectIdNumberAndDeptIdList();

}